/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.common.util;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import java.io.File;
import java.io.IOException;

/**
 * @author: created  Date: 16/12/20 Time: 下午3:13
 * @version: 1.0
 */
public final class FileUtil {

    private static Supplier<String> store = Suppliers.memoize(new Supplier<String>() {
        @Override
        public String get() {
            String path = System.getProperty("cgmnx.cache", null);

            if (path == null) {
                path = System.getProperty("catalina.base");
                if (path == null) {
                    path = System.getProperty("java.io.tmpdir");
                }
                path = path + File.separator + "cache";

                System.setProperty("cgmnx.cache", path);
            }

            File file = new File(path);
            file.mkdirs();

            try {
                //返回的是规范化的绝对路径，相当于将getAbsolutePath()中的“.”和“..”解析成对应的正确的路径
                path = file.getCanonicalPath();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return path;
        }
    });

    public static String getStore() {
        return store.get();
    }
}